package com.wink.file.center.service;

import com.wink.file.center.config.LocalOSSConfig;
import com.wink.file.center.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 本地oss服务impl
 *
 * @author xw
 * @date 2020-11-17 17:57
 */
@Slf4j
@Service
public class LocalOssServiceImpl implements FileService {

    private static ExecutorService threadPool = Executors.newCachedThreadPool();


    @Override
    public Map<String, String> upload(MultipartFile file, String fileName) {
        fileName = createFileName(file, fileName);
        String path = LocalOSSConfig.path + fileName;
        threadPool.execute(() -> FileUtil.saveFile(file, path));

        Map<String, String> map = new HashMap<>(2);
        map.put("url", LocalOSSConfig.domain + fileName);
        map.put("path", path);
        map.put("message", "上传中请稍后访问！");
        log.warn("LocalOss OSS:{}",LocalOSSConfig.domain + fileName);
        return map;
    }

    @Override
    public void delete(String id) {

    }
}
